package com.cqupt.controller;


import com.cqupt.pojo.*;
import com.cqupt.service.BasicalItemService;
import com.cqupt.service.PortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("Port")
@ResponseBody
public class PortController {

    private final PortService portService;
    private final BasicalItemService basicalItemService;

    @Autowired
    public PortController(PortService portService, BasicalItemService basicalItemService) {
        this.portService = portService;
        this.basicalItemService = basicalItemService;
    }



    /**
     * 获取所有的SNMP设备
     * @return 查询的结果集
     */
    @RequestMapping("/get_all_SNMP_devices")
    public ResponseMessage getAllSNMPDevices(){
        ResponseMessage responseMessage = new ResponseMessage();
        final List<Map<String, Object>> resultList = portService.getAllSNMPDevices();
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }

    /**
     * 获取端口数量
     * @param hostId 主机的ID值
     * @return 端口数
     */
    @RequestMapping("/get_all_port")
    public Map<String,Map<String,Map<String,Object>>> getAllPort(@RequestParam(value = "hostId", required = false) String hostId){
        Map<String, Map<String,Object>> result = portService.getAllPort(hostId);
//        System.out.println("陈下:"+result);
        Map<String,Map<String, Map<String,Object>>> returnResult = new HashMap<>();
        returnResult.put("result",result);
        return returnResult;
    }

/*    @RequestMapping("/get_port_info")
    private ResponseMessage getPortInfo(@RequestParam(value = "hostId",required = false) String hostId,
                                        @RequestParam(value = "applicationid",required = false) String applicationid
    ){
        ResponseMessage responseMessage = new ResponseMessage();
        final List<Map<String, Object>> resultList = portService.getPortInfo(hostId,applicationid);
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", resultList);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        return responseMessage;
    }*/

    /*    *//**
     已废弃
     * 通过主机获取负载信息、版本号、温度
     * @param hostId 选择的hostId
     * @return 封装的返回消息，含result(所有结果)和dataNum(数据总数)
     *//*
    @RequestMapping("/get_snmp_information")
    public ResponseMessage getSnmpHwEntityInformation(@RequestParam(value = "hostId", required = false) String hostId
    ) {
        ResponseMessage responseMessage = new ResponseMessage();
        //SNMP设备硬件信息
        List<Map<String, Object>> info = portService.getInfomtionByhostId(hostId);
        //SNMP设备负载
        List<Map<String, Object>> cpuInfo = basicalItemService.getUtilizationByName(hostId,"CPU utilization");
        List<Map<String, Object>> mpuInfo = basicalItemService.getUtilizationByName(hostId,"MPU Board");
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for(int i = 0;i< info.size();i++){
            result.add(info.get(i));
        }
        result.add(cpuInfo.get(cpuInfo.size()-1));
        result.add(mpuInfo.get(mpuInfo.size()-1));
        System.out.println("CPU:"+info.toString());
        Map<String, Object> resultMap = new HashMap<String, Object>(){{
            put("result", result);
        }};
        responseMessage.setStatus(true);
        responseMessage.setContent(resultMap);
        System.out.println(responseMessage.toString());
        return responseMessage;
    }*/

    /**
     * 仅获取硬件信息，如序列号、温度等
     * @param hostId 指定主机hostId
     * @return 封装的信息
     */
    @RequestMapping("/get_snmp_information")
    public ResponseMessage getHardwareInformation(@RequestParam(value = "hostId",required = false) String hostId){
        ResponseMessage responseMessage = new ResponseMessage();
        //SNMP设备硬件信息
//        System.out.println("测试");
        Map<String,String> info = portService.getInfomtionByhostId(hostId);
        Map<String, Object> map = new HashMap<>();
        map.put("result",info);
        responseMessage.setStatus(true);
        responseMessage.setContent(map);
//        System.out.println("往回前台的数据"+map.get("result"));
        return responseMessage;
    }

    /**
     *  获取snmp设备的利用率相关（如cpu、内存利用率）
     * @param hostId 指定主机的hostId
     * @return 封装的消息
     */
    @RequestMapping("/getUtilization")
    public ResponseMessage getUtilization(@RequestParam(value = "hostId",required = false)  String hostId){
        ResponseMessage responseMessage = new ResponseMessage();
        // SNMP设备利用率相关
        List<Map<String, Object>> cpuInfo = basicalItemService.getUtilizationByName(hostId,"CPU utilization",null);
        List<Map<String, Object>> mpuInfo = basicalItemService.getUtilizationByName(hostId,"MPU Board",null);
        responseMessage.setStatus(true);
        Map<String,Object> map = new HashMap<>();
        map.put("cpuInfo",cpuInfo);
        map.put("mpuInfo",mpuInfo);
        responseMessage.setContent(map);
        return responseMessage;
    }
}
